﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using Devv.Core.Utils;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public abstract class BaseEntity
	{
		#region Properties

		public Int32 Id { get; set; }
		public Boolean Active { get; set; }

		#endregion

		#region Constructor helpers

		public void Construct(SqlDataReader reader)
		{
			if (reader == null || !reader.Read())
			{
				return;
			}

			Load(reader);

			if (!reader.IsClosed)
			{
				reader.Close();
			}

			reader.Dispose();
		}

		public abstract void Load(SqlDataReader reader);

		#endregion

		#region Methods

		protected String GetCode(String value)
		{
			String code = RegexUtil.RemoveSpecialCharacters(value, true).Replace(" ", "-");

			while (code.Contains("--"))
			{
				code = code.Replace("--", "-");
			}

			while (code.Substring(code.Length - 1) == ".")
			{
				code = code.Substring(0, code.Length - 1);
			}

			return Id + "-" + code;
		}

		#endregion
	}
}